(function(){
    var heught_l=$(".lian_seach").height();
    $(".du_seach").css("height",heught_l);
    $.ajax({
        type: 'POST',
        url: '/admin/GW/twoCascades',
        dataType: 'JSON',
        data: {},
        success: function(data){
            var province  = data[0];
            for (var i  in province) {
                var str = "<option value='"+i+"'>"+province[i]+"</option>";
                $('.province').append(str);
            }
            var city = data[1];
            $('.province').change(function(){
                var index = this.selectedIndex;
                var children = $('.city').children();
                for (var i = 0; i < children.length; i++) {
                    children[i].remove();
                }
                var str = "<option value='0'>全部</option>";
                $('.city').append(str);
                for (var i in city[index - 1]) {
                    var str = "<option value='"+i+"'>"+city[index - 1][i]+"</option>";
                    $('.city').append(str);
                }
            });
        }
    });
    getOrder();
}());

window.onresize = function() {
    var heught_l = $(".lian_seach").height();
    $(".du_seach").css("height", heught_l);
};

var successStatus = 0;
$('.radio').click(function () {
    var value = $(this).val();
    successStatus = value;
    switch(value) {
        case "0":
            $('.month_a').attr("disabled", "disabled");
            $('.month_b').attr("disabled", "disabled");
            $('.select_time').removeAttr("disabled");
            break;
        case "1":
            $('.select_time').attr("disabled", "disabled");
            $('.month_a').removeAttr("disabled");
            $('.month_b').removeAttr("disabled");
            break;
    }
});

var overdueStatus = 0;
$('.radio-overdue').click(function () {
    var value = $(this).val();
    overdueStatus = value;
    switch (value) {
        case "0":
            $('.month_overdue_a').attr("disabled", "disabled");
            $('.month_overdue_b').attr("disabled", "disabled");
            $('.select_time_overdue').removeAttr("disabled");
            break;
        case "1":
            $('.select_time_overdue').attr("disabled", "disabled");
            $('.month_overdue_a').removeAttr("disabled");
            $('.month_overdue_b').removeAttr("disabled");
            break;
    }
});
// $('.radio').on("click",function () {
//     var value = $(this).val();
//     if(value==0){
//         $('.month_a').attr("disabled", "disabled");
//         $('.month_b').attr("disabled", "disabled");
//         $('.select_time').removeAttr("disabled");
//     }else if(value==1){
//         $('.select_time').attr("disabled", "disabled");
//         $('.month_a').removeAttr("disabled");
//         $('.month_b').removeAttr("disabled");
//     }
// });
//
// $('.radio-overdue').on("click",function () {
//     var value = $(this).val();
//     if(value==0) {
//         $('.month_overdue_a').attr("disabled", "disabled");
//         $('.month_overdue_b').attr("disabled", "disabled");
//         $('.select_time_overdue').removeAttr("disabled");
//     }else if(value==1){
//         $('.select_time_overdue').attr("disabled", "disabled");
//         $('.month_overdue_a').removeAttr("disabled");
//         $('.month_overdue_b').removeAttr("disabled");
//     }
// });

function getOrder()
{
    var usernum = window.location.search.substring(1);
    if (usernum == 'status=2') return;
    $.ajax({
        type: 'POST',
        url: '/admin/GW/getAllGwPersonnelList',
        dataType: 'JSON',
        data: {usernum: usernum, status: 0},
        success: function (res) {
            if (res.error == 1 || res.msg.length <= 0) {
                addNotify();
                return;
            }
            var data = res.msg;
            setItem('orders', data);
            var totalPage = Math.ceil(data.length / 15);
            showPage(1, totalPage, addInfo);recovery();
            addInfo(1, totalPage);
        },
        error: function (error) {
            console.log(error);
        }
    });
}

$('.find_btn_seach').click(function () {
    var usernum = $('.gwusernum').val();
    if (usernum == '') {
        sweetAlert('', '岗位号不能为空', 'error');
        return;
    }
    $.ajax({
        type: 'POST',
        url: '/admin/GW/getAllGwPersonnelList',
        dataType: 'JSON',
        data: {usernum: usernum, status: 0},
        success: function (res) {
            if (res.error == 1 || res.msg.length <= 0) {
                addNotify();
                return;
            }
            var data = res.msg;
            setItem('orders', data);
            var totalPage = Math.ceil(data.length / 15);
            showPage(1, totalPage, addInfo);recovery();
            addInfo(1, totalPage);
        },
        beforeSend: function() {
            $('.modal_Load').show();
        },
        complete: function() {
            $('.modal_Load').hide();
        },
        error: function (error) {
            console.log(error);
        }
    });
});


$('.lian_btn_seach').click(function () {
    getSuccessOverdueOrder(1);
});

function getSuccessOverdueOrder(page){
    var month_a = $('.month_a').val();
    var month_b = $('.month_b').val();
    if ((!isIntNum(month_a) || month_a == '') && successStatus == 1) {
        sweetAlert('', '起始月不能为空或请输入数字', 'error');
        return;
    }
    if ((!isIntNum(month_b) || month_b == '') && successStatus == 1) {
        sweetAlert('', '结束月不能为空或请输入数字', 'error');
        return;
    }
    if (parseInt(month_a) > parseInt(month_b) && successStatus == 1) {
        sweetAlert('', '起始月不能大于结束月', 'error');
        return;
    }
    if (month_a <= 0 && successStatus == 1) {
        sweetAlert('', '起始月不能小于零', 'error');
        return;
    }
    if (successStatus == 0) {
        var select_time = $('.select_time').val();
        if (select_time == 0) {
            var successMonths = [1, 12];
        } else {
            var successMonths = [select_time, select_time];
        }
    } else if (successStatus == 1) {
        var successMonths = [month_a, month_b];
    }

    var successl = [0, 100];
    var success_a = $('.success_a').val();
    var success_b = $('.success_b').val();
    if (!isIntNum(success_a) && success_a != '') {
        sweetAlert('', '请输入数字', 'error');
        return;
    }
    if (!isIntNum(success_b) && success_b != '') {
        sweetAlert('', '请输入数字', 'error');
        return;
    }
    if (parseInt(success_a) > parseInt(success_b)) {
        sweetAlert('', '起始数值不能大于结束数值', 'error');
        return;
    }
    if (success_a != '' && success_b != '') {
        successl = [success_a, success_b];
    }
    var successtag = [];
    var tag_a = $('.tag_a').val();
    var tag_b = $('.tag_b').val();
    if (!isIntNum(tag_a) && tag_a != '') {
        sweetAlert('', '请输入数字', 'error');
        return;
    }
    if (!isIntNum(tag_b) && tag_b != '') {
        sweetAlert('', '请输入数字', 'error');
        return;
    }
    if (parseInt(tag_a) > parseInt(tag_b)) {
        sweetAlert('', '起始数值不能大于结束数值', 'error');
        return;
    }
    if (tag_a != '' && tag_b != '') {
        successtag = [tag_a, tag_b];
    }


    var month_overdu_a = $('.month_overdue_a').val();
    var month_overdu_b = $('.month_overdue_b').val();
    if ((!isIntNum(month_overdu_a) || month_overdu_a == '') && overdueStatus == 1) {
        sweetAlert('', '起始月不能为空或请输入数字', 'error');
        return;
    }
    if ((!isIntNum(month_overdu_b) || month_overdu_b == '') && overdueStatus == 1) {
        sweetAlert('', '结束月不能为空或请输入数字', 'error');
        return;
    }
    if (parseInt(month_overdu_a) > parseInt(month_overdu_b) &&  overdueStatus == 1) {
        sweetAlert('', '起始月不能大于结束月', 'error');
        return;
    }
    if (month_overdu_a <= 0 &&  overdueStatus == 1) {
        sweetAlert('', '起始月不能小于零', 'error');
        return;
    }
    if (overdueStatus == 0) {
        var select_time_overdue = $('.select_time_overdue').val();
        if (select_time_overdue == 0) {
            var overdueMonths = [1, 12];
        } else {
            var overdueMonths = [select_time_overdue, select_time_overdue];
        }
    } else if (overdueStatus == 1) {
        var overdueMonths = [month_overdu_a, month_overdu_b];
    }

    var overduel = [0, 100];
    var overude_a = $('.overdue_a').val();
    var overude_b = $('.overdue_b').val();
    if (!isIntNum(overude_a) && overude_a != '') {
        sweetAlert('', '请输入数字', 'error');
        return;
    }
    if (!isIntNum(overude_b) && overude_b != '') {
        sweetAlert('', '请输入数字', 'error');
        return;
    }
    if (parseInt(overude_a) > parseInt(overude_b)) {
        sweetAlert('', '起始数值不能大于结束数值', 'error');
        return;
    }
    if (overude_a != '' && overude_b != '') {
        overduel = [overude_a, overude_b];
    }
    var overduetag = [];
    var tag_overdue_a = $('.tag_overdue_a').val();
    var tag_overdue_b = $('.tag_overdue_b').val();
    if (!isIntNum(tag_overdue_a) && tag_overdue_a != '') {
        sweetAlert('', '请输入数字', 'error');
        return;
    }
    if (!isIntNum(tag_overdue_b) && tag_overdue_b != '') {
        sweetAlert('', '请输入数字', 'error');
        return;
    }
    if (parseInt(tag_overdue_a) > parseInt(tag_overdue_b)) {
        sweetAlert('', '起始数值不能大于结束数值', 'error');
        return;
    }
    if (tag_overdue_a != '' && tag_overdue_b != '') {
        overduetag = [tag_overdue_a, tag_overdue_b];
    }
    var cityId = $('.city').val();
    var gwStatus = $('.gw_status').val();
    var provinceId = $('.province').val();

    var data = {
        status: 1,
        province: provinceId,
        city: cityId,
        gwStatus: gwStatus,
        successMonths: successMonths,
        successl: successl,
        successtag: successtag,
        overdueMonths: overdueMonths,
        overduel: overduel,
        overduetag: overduetag
    };
    $.ajax({
        type: 'POST',
        url: '/admin/GW/getAllGwPersonnelList',
        dataType: 'JSON',
        data: data,
        success: function (res) {
            if (res.error == 1 || res.msg.length <= 0) {
                addNotify();
                return;
            }
            var data = res.msg;
            setItem('orders', data);
            var totalPage = Math.ceil(data.length / 15);
            showPage(1, totalPage, addInfo);recovery();
            addInfo(1, totalPage);
        },
        beforeSend: function() {
            $('.modal_Load').show();
        },
        complete: function() {
            $('.modal_Load').hide();
        },
        error: function (error) {
            console.log(error);
        }
    });
}
function addInfo(page, totalPage) {
    var data = pageData(page);
    showPage(page, totalPage, addInfo);
    if (data.length <= 0) {
        addNotify();
        return;
    }
    $('.table_hover tr').remove();
    $('.tr_item_title th').remove();
    $('.tr_checkbox th').remove();
    var th = '';
    var tr = '';
    tr += "<th>序号</th> <th>当前状态</th> <th>入职时间</th>";
    tr += "<th>GW姓名</th> <th>GW岗位号</th><th>城市</th> ";
    tr += "<th>CS岗位号</th> <th>省份</th> <th>SZ岗位号</th>";
    tr += "<th>总过单数</th> <th>总过单比</th> <th>总过单率</th>";
    th += " <th><input type='checkbox' class='checkbox_input'></th><th><input type='checkbox' class='checkbox_input'></th>";
    th += " <th><input type='checkbox' class='checkbox_input'></th><th><input type='checkbox' class='checkbox_input'></th>";
    th += " <th><input type='checkbox' class='checkbox_input'></th><th><input type='checkbox' class='checkbox_input'></th>";
    th += " <th><input type='checkbox' class='checkbox_input'></th><th><input type='checkbox' class='checkbox_input'></th>";
    th += " <th><input type='checkbox' class='checkbox_input'></th><th><input type='checkbox' class='checkbox_input'></th>";
    th += " <th><input type='checkbox' class='checkbox_input'></th><th><input type='checkbox' class='checkbox_input'></th>";
    var trStatus = true; var colorTag = false;
    for (var i = 0; i < data.length; i++) {
        var status = '';
        if (data[i].status == 1) status = '禁用';
        else status = '正常';
        var str = '';
        str += "<td>" + (i + 1) + "</td>";
        str += "<td>" + status + "</td>";
        str += "<td>" + data[i].time + "</td>";
        str += "<td>" + data[i].gwname + "</td>";
        str += "<td>" + data[i].gwusernum + "</td>";
        str += "<td>" + data[i].city + "</td>";
        str += "<td>" + data[i].csusernum + "</td>";
        str += "<td>" + data[i].province + "</td>";
        str += "<td>" + data[i].szusernum + "</td>";
        var orders = data[i].successOrders;
        if (orders.length <= 0) {
            var successTimes = data[i].successTimes;
            for (var j = parseInt(successTimes[0]); j <= parseInt(successTimes[1])+1; j ++) {
                str += "<td>暂无数据</td>";
                str += "<td>暂无数据</td>";
                str += "<td>暂无数据</td>";
            }
        }
        for (var j = 0; j < orders.length; j++) {
            if (j == 0) {
                str += "<td>" + orders[j].orderTotalCount + "</td>";
                str += "<td>" + orders[j].orderTotalRatio + "</td>";
                str += "<td>" + orders[j].orderTotalRate.toFixed(2) + "%</td>";

            }else if (j != 0) {
                str += "<td>" + orders[j].orderCount + "</td>";
                str += "<td>" + orders[j].orderRatio + "</td>";
                str += "<td>" + orders[j].orderRate.toFixed(2) + "%</td>";
            }
            if (orders[j].tag) {
                colorTag = true;
            }
        }

        var orders = data[i].overdueOrders;
        if (orders.length <= 0) {
            var overdueTimes = data[i].overdueTimes;
            for (var j = parseInt(overdueTimes[0]); j <= parseInt(overdueTimes[1])+1; j ++) {
                str += "<td>暂无数据</td>";
                str += "<td>暂无数据</td>";
                str += "<td>暂无数据</td>";
            }
        }
        for (var j = 0; j < orders.length; j++) {
            if (j == 0) {
                str += "<td>" + orders[j].orderTotalCount + "</td>";
                str += "<td>" + orders[j].orderTotalRatio + "</td>";
                str += "<td>" + orders[j].orderTotalRate.toFixed(2) + "%</td>";
            } else if (j != 0) {
                str += "<td>" + orders[j].orderCount + "</td>";
                str += "<td>" + orders[j].orderRatio + "</td>";
                str += "<td>" + orders[j].orderRate.toFixed(2) + "%</td>";
            }
            if (orders[j].tag) {
                colorTag = true;
            }
        }
        if (trStatus) {
            var successTimes = data[i].successTimes;
            for (var j = parseInt(successTimes[0]); j <= parseInt(successTimes[1]); j ++) {
                var month = '';
                if (j == 0) {
                    month = '当月';
                } else if (j == 1) {
                    month = '上月';
                } else {
                    month = '第'+j+'月';
                }
                th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                tr += "<th>" + month+ "过单数</th>";
                tr += "<th>" + month+ "过单比</th>";
                tr += "<th>" + month+ "过单率</th>";
            }

            var overdueTimes = data[i].overdueTimes; var tag = true;
            for (var j = parseInt(overdueTimes[0]); j <= parseInt(overdueTimes[1]); j ++) {
                if (tag) {
                    th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                    th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                    th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                    tr+= "<th>总逾期数</th> <th>总逾期比</th> <th>总逾期率</th>";
                }
                var month = '';
                if (j == 1) {
                    month = '当月';
                } else if (j == 2) {
                    month = '上月';
                } else {
                    month = '第'+j+'月';
                }
                th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                tr += "<th>" + month+ "逾期数</th>";
                tr += "<th>" + month+ "逾期比</th>";
                tr += "<th>" + month+ "逾期率</th>";
                tag = false;
            }
        }
        trStatus = false;
        var style = colorTag ? 'color:red' : '';
        var str2 = "<tr style='"+style+"'>" + str + "</tr>";
        $('.table_hover').append(str2);
    }
    th += "<th calss=\"btn_rate btn_filter\" id=\"btn_filter\"><button type='button' class='btn btn-warning btn_filter' >过滤</button></th>";
    th += "<th calss=\"btn_recovery\" id=\"btn_recovery\"><button type='button' class='btn btn-warning btn_recovery' >重置</button></th>";
    $('.tr_item_title').append(tr);
    $('.tr_checkbox').append(th);
    if (!filterTag) {
        tableHide();
    } else {
        recovery();
    }
}

$('.button_export').click(function(){
    var status = $(this).val();
    $.ajax({
        type: 'POST',
        url: '/admin/GW/exportToExcelAllPersonnel',
        dataType: 'JSON',
        data: {status: status, filterIndexs: filterIndexs},
        success: function(response){
            location.href = response.path;
        },
        beforeSend: function() {
            $('.modal_Load').show();
        },
        complete: function() {
            $('.modal_Load').hide();
        },
        error: function(data){
            console.log(data);
        }
    });

});

// $('.button_export_curr').click(function(){
//     return ExcellentExport.excel(this, 'datatable', '当前报表');
// });